package com.framework.component.extension.register;

import com.framework.component.extension.anno.Extension;
import com.framework.component.extension.anno.ExtensionPoint;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Map;

@Component
public class ExtensionBootstrap implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    private final ExtensionRegister register;

    public ExtensionBootstrap(ExtensionRegister register) {
        this.register = register;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @PostConstruct
    public void init() {
        Map<String, Object> beansWithAnnotation = applicationContext.getBeansWithAnnotation(Extension.class);
        beansWithAnnotation.values().forEach(extension -> register.register((ExtensionPoint) extension));
    }
}
